<template>
  <div>
    <el-row>
      <el-col :span="24">
        <el-form :inline="true" :model="dataForm">
          <el-form-item label="分类">
            <category-cascader :categoryPath.sync="categoryPath"></category-cascader>
          </el-form-item>
          <el-form-item label="选择课程">
            <course-select style="width:160px"></course-select>
          </el-form-item>
          <el-form-item label="检索">
            <el-input style="width:160px" v-model="dataForm.key" clearable></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="searchAttrsInfo">查询</el-button>
          </el-form-item>
        </el-form>
      </el-col>
      <el-col :span="24">
        <!-- 折叠面板 -->
        <!-- <attrsupdate :catId="catId"></attrsupdate> -->
        <el-table :data="dataList" border v-loading="dataListLoading" style="width: 100%;">
          <el-table-column prop="courseId" header-align="center" align="center" label="#"></el-table-column>
          <el-table-column prop="name" header-align="center" align="center" label="课程名字"></el-table-column>
          <el-table-column prop="categoryId" header-align="center" align="center" label="分类id"></el-table-column>
          <el-table-column prop="catname" header-align="center" align="center" label="分类名字"></el-table-column>
          <el-table-column prop="logo" header-align="center" align="center" label="课程图片地址">
            <template slot-scope="scope">
              <img :src="scope.row.logo" style="width: 100px; height: 80px" />
            </template>
          </el-table-column>
          <el-table-column prop="descript" header-align="center" align="center" label="课程介绍" show-overflow-tooltip></el-table-column>
          <el-table-column prop="courseHeat" header-align="center" align="center" label="课程热度"></el-table-column>
          <el-table-column
            prop="showStatus"
            header-align="center"
            align="center"
            label="状态"
          >
            <template slot-scope="scope">
              <el-tag type="success" v-if="scope.row.showStatus==0">禁用</el-tag>
              <el-tag v-else>正常</el-tag>
            </template>
          </el-table-column>
          <el-table-column
            prop="esType"
            header-align="center"
            align="center"
            label="上线状态"
          >
            <template slot-scope="scope">
              <el-tag type="success" v-if="scope.row.esType==0">新建</el-tag>
              <el-tag v-else>上线</el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="gmtCreate" header-align="center" align="center" label="注册时间"></el-table-column>
          <el-table-column prop="gmtModified" header-align="center" align="center" label="更新时间"></el-table-column>
          <el-table-column
            fixed="right"
            header-align="center"
            align="center"
            width="150"
            label="操作"
          >
            <template slot-scope="scope">
              <el-button type="text" size="small" @click="courseEsType(scope.row)">上线</el-button>
              <el-button type="text" size="small" @click="attrUpdateShow(scope.row)">属性管理</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          @size-change="sizeChangeHandle"
          @current-change="currentChangeHandle"
          :current-page="pageIndex"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="pageSize"
          :total="totalPage"
          layout="total, sizes, prev, pager, next, jumper"
        ></el-pagination>
      </el-col>
    </el-row>
  </div>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';
import CategoryCascader from "../common/category-cascader";
import CourseSelect from "../common/course-select";
// import attrsupdate from "./attrsupdate";, attrsupdate
export default {
  //import引入的组件需要注入到对象中才能使用
  components: { CategoryCascader, CourseSelect },
  data() {
    //这里存放数据
    return {
      catId: 0,
      categoryPath: [],
      dataForm: {
        key: "",
        courseId: 0,
        categoryId: 0
      },
      catPathSub: null,
      courseIdSub: null,
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false
    };
  },
  methods: {
    courseEsType(row) {
      this.$http({
        url: this.$http.adornUrl(`/ly/classcourseestype/${row.courseId}/up`),
        method: "post"
      }).then(({ data }) => {
        if (data.code == 0) {
          this.$message({
            type: "success",
            message: "上传服务成功!"
          });
          this.getDataList();
        } else {
          this.$message({
            type: "error",
            message: "保存失败，原因【" + data.msg + "】"
          });
        }
      });
    },
    searchAttrsInfo() {
      console.log("搜索条件", this.dataForm);
      this.getDataList();
      // this.PubSub.publish("dataForm",this.dataForm);
    },
    attrUpdateShow(row) {
      console.log(row);
      this.$router.push({
        path: "/class-attrsupdate",
        query: { courseId: row.courseId, categoryId: row.categoryId }
      });
    },
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true;
      let param = {};
      Object.assign(param, this.dataForm, {
        page: this.pageIndex,
        limit: this.pageSize
      });
      this.$http({
        url: this.$http.adornUrl("/ly/classcoursecategory/list/info"),
        method: "get",
        params: this.$http.adornParams(param)
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.dataList = data.page.list;
          this.totalPage = data.page.totalCount;
        } else {
          this.dataList = [];
          this.totalPage = 0;
        }
        this.dataListLoading = false;
      });
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    }
  },
  activated() {
    this.getDataList();
  },

  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {
    this.catPathSub = PubSub.subscribe("catPath", (msg, val) => {
      this.dataForm.categoryId = val[val.length - 1];
    });
    this.courseIdSub = PubSub.subscribe("courseId", (msg, val) => {
      this.dataForm.courseId = val;
    });
  },
  beforeDestroy() {
    PubSub.unsubscribe(this.catPathSub);
    PubSub.unsubscribe(this.courseIdSub);
  }
};
</script>
<style scoped>
</style>